<template>
  <div class="app-container" ref="appContainer">
    <PropTable
      ref="tableData"
      :loading="loading"
      :columns="column"
      :data="data"
      @reset="reset"
      @onSubmit="onSubmit"
      @add="add"
      @change="changeState"
    >
      <template v-slot:operation="scope">
        <el-button type="primary" size="small" icon="Edit" @click="edit(scope.row)">
          编辑
        </el-button>
      </template>
    </PropTable>
    <el-drawer v-model="dialogVisible" title="编辑">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
      >
        <el-form-item label="文件类型：" prop="type">
          <el-input v-model="ruleForm.type" placeholder="请输入文件类型" />
        </el-form-item>
        <el-form-item label="文件分类：" prop="stage">
          <el-select
            v-model="ruleForm.stage"
            placeholder="请选择文件分类"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="ite in fileTypeList"
              :key="ite.value"
              :label="ite.label"
              :value="ite.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="ruleForm.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleClose(ruleFormRef)">确定</el-button>
        </span>
      </template>
    </el-drawer>

    <el-drawer v-model="dialogVisible2" title="新增">
      <el-form
        ref="ruleFormRef2"
        :model="newFileType"
        :rules="rules2"
        label-width="120px"
        class="demo-ruleForm2"
        :size="formSize"
      >
        <el-form-item label="文件类型：" prop="type" placeholder="请输入文件类型">
          <el-input v-model="newFileType.type" placeholder="请输入文件类型" />
        </el-form-item>
        <el-form-item label="文件分类：" prop="stage">
          <el-select
            v-model="newFileType.stage"
            placeholder="请选择文件分类"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="ite in fileTypeList"
              :key="ite.value"
              :label="ite.label"
              :value="ite.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否必需：" prop="isRequired">
          <el-select
            v-model="newFileType.isRequired"
            placeholder="请选择是否必需"
            style="width: 100%"
            filterable
          >
            <el-option
              v-for="ite in ifList"
              :key="ite.value"
              :label="ite.label"
              :value="ite.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="newFileType.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible2 = false">取消</el-button>
          <el-button type="primary" @click="addFileType(ruleFormRef2)">确定</el-button>
        </span>
      </template>
    </el-drawer>
  </div>
</template>
<script lang="ts" setup name="comprehensive">
  import { ref, reactive, onMounted, nextTick } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import type { FormInstance } from 'element-plus'
  import {
    postFileTypeSearch,
    postFileType,
    putFileType,
  } from '@/api/controller/file-type-controller/index'
  import { getSysFileTypeList } from '@/api/controller/sys-user-controller/index'

  const loading = ref(true)
  const appContainer = ref(null)
  import PropTable from '@/components/Table/PropTable/index.vue'

  const column = ref([
    { name: 'type', label: '文件类型', inSearch: true, valueType: 'input' },
    {
      name: 'stage',
      label: '文件分类',
      inSearch: true,
      valueType: 'select',
      width: 300,
      options: [],
    },
    { name: 'state', label: '状态', valueType: 'input', itemIf: true, width: 140 },
    { name: 'isRequired', label: '是否必需', valueType: 'input', itemIf: true, width: 140 },
    { name: 'remark', label: '备注', valueType: 'input' },
    { name: 'operation', slot: true, fixed: 'right', width: 140, label: '操作', type: false },
  ])
  const data = ref(null)

  const formSize = ref('default')
  const ruleFormRef = ref<FormInstance>()
  const ruleFormRef2 = ref<FormInstance>()
  const ruleForm = ref({
    type: null,
    stage: null,
    remark: null,
  })

  const rules = reactive({
    type: [
      { required: true, message: '请输入文件类型', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    stage: [{ required: true, message: '请选择文件类型', trigger: 'blur' }],
    remark: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
  })

  const rules2 = reactive({
    type: [
      { required: true, message: '请输入文件类型', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    stage: [{ required: true, message: '请选择文件类型', trigger: 'blur' }],
    isRequired: [{ required: true, message: '请选择是否必需', trigger: 'blur' }],
    remark: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
  })
  const dialogVisible = ref(false)
  const dialogVisible2 = ref(false)
  const tableData = ref(null)
  //修改
  const handleClose = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        const result = await putFileType(ruleForm.value, { loading: true })
        if (result.code === 200) {
          try {
            loading.value = true
            const result = await postFileTypeSearch(tableData.value?.formInline)
            data.value = result.data
            dialogVisible.value = false
            ElMessage.success('修改成功')
          } finally {
            loading.value = false
          }
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }
  // 打开新增弹窗
  const ifList = ref([
    {
      label: '是',
      value: 1,
    },
    {
      label: '否',
      value: 0,
    },
  ])
  const newFileType = ref({
    type: null,
    isRequired: null,
    state: 1,
    remark: null,
    stage: null,
  })
  const add = () => {
    newFileType.value = {
      type: null,
      isRequired: null,
      state: 1,
      remark: null,
      stage: null,
    }
    dialogVisible2.value = true
    nextTick(() => {
      ruleFormRef2.value.clearValidate()
    })
  }

  //新增
  const addFileType = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        const result = await postFileType(newFileType.value, { loading: true })
        if (result.code === 200) {
          try {
            loading.value = true
            const result = await postFileTypeSearch(tableData.value?.formInline)
            data.value = result.data
            dialogVisible2.value = false
            ElMessage.success('添加成功')
          } finally {
            loading.value = false
          }
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }

  //打开编辑弹窗
  const edit = (row) => {
    ruleForm.value = { ...row }
    dialogVisible.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }
  // 删除当前项
  const del = async (row) => {
    ElMessageBox.confirm('你确定要删除当前项吗?', '温馨提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      draggable: true,
    })
      .then(async () => {
        const result = await deleteClientById({ id: row.id })
        if (result.code === 200) {
          loading.value = true
          const result = await postFileTypeSearch(tableData.value?.formInline)
          data.value = result.data
          dialogVisible.value = false
          loading.value = false
          ElMessage.success('删除成功')
        }
      })
      .catch(() => {
        console.log('错误')
      })
  }
  //重置搜索条件
  const reset = async () => {
    try {
      loading.value = true
      let pageInfo = {
        pageNum: 1,
        pageSize: 15,
      }
      const result = await postFileTypeSearch(pageInfo)
      data.value = result.data
    } finally {
      loading.value = false
    }
  }
  //搜索
  const onSubmit = async (val) => {
    try {
      loading.value = true
      const result = await postFileTypeSearch(val)
      data.value = result.data
    } finally {
      loading.value = false
    }
  }
  const fileTypeList = ref([])
  //初始化
  onMounted(async () => {
    try {
      loading.value = true
      let pageInfo = {
        pageNum: 1,
        pageSize: 15,
      }
      let result = await postFileTypeSearch(pageInfo)
      data.value = result.data
      result = await getSysFileTypeList()
      fileTypeList.value = result.data
      column.value[1].options = result.data
    } finally {
      loading.value = false
    }
  })
  const changeState = async (id, enabled, str) => {
    let upDatas = null
    if (str === 'state') {
      if (enabled) {
        upDatas = {
          id: id,
          state: 1,
        }
      } else {
        upDatas = {
          id: id,
          state: 0,
        }
      }
    } else {
      if (enabled) {
        upDatas = {
          id: id,
          isRequired: 1,
        }
      } else {
        upDatas = {
          id: id,
          isRequired: 0,
        }
      }
    }
    const res = await putFileType(upDatas, { loading: true })
    ElMessage.success(res.data)
  }
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .app-container {
    flex: 1;
    display: flex;
    width: 100%;
    padding: 16px;
    box-sizing: border-box;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
